<template>
  <div class="add-user">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="ruleForm.nickName"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPassword">
        <el-input
          type="password"
          v-model="ruleForm.userPassword"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "user-add",
  data() {
    var checkNickName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("昵称不能为空"));
      } else {
        const find = { nickName: value };
        this.$store.dispatch("users/search", find).then((res) => {
          if (res.length != 0) {
            return callback(new Error("昵称已被使用"));
          }
          return callback();
        });
      }
    };
    var checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      } else {
        const find = { userName: value };
        this.$store.dispatch("users/search", find).then((res) => {
          if (res.length != 0) {
            return callback(new Error("账号已被使用"));
          }
          callback();
        });
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.userPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        nickName: "",
        userPassword: "",
        checkPass: "",
        userName: "",
      },
      rules: {
        userPassword: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        userName: [{ validator: checkUserName, trigger: "blur" }],
        nickName: [{ validator: checkNickName, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { nickName, userName, userPassword } = this.ruleForm;
          console.log({ nickName, userName, userPassword });
          this.$store
            .dispatch("users/reg", { nickName, userName, userPassword })
            .then((res) => {
              console.log(res);
              if (res) {
                this.$message({
                  message: "注册成功",
                  type: "success",
                });
                // this.$router.push("/users");
              }
              this.ruleForm.userPassword = this.ruleForm.userName = this.ruleForm.checkPass = this.ruleForm.nickName =
                "";
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.add-user {
  width: 50%;
  margin: 6% auto;
}
</style>